// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import Common from "../Common";
import {isEmpty} from "../common/Helpers";

const {ccclass, property} = cc._decorator;

@ccclass
export default class LoginDialog extends Common {

    @property({
        type: cc.Node,
        displayName: '登录按钮'
    })
    loginBtn: cc.Node;

    protected formData: any = {
        username: '',
        password: '',
    }



    start() {
        this.loginBtn.on(cc.Node.EventType.TOUCH_START, this.onLogin, this)
    }


    async onLogin() {
        this.onBtnClick()
        const formData = this.formData;
        if (isEmpty(formData.username)) {
            return this.showToast('请输入用户名')
        }
        if (isEmpty(formData.password)) {
            return this.showToast('请输入密码')
        }
        try {
            const {message, data} = await this.http.post('auth/login', formData)
            this.showToast(message);
            this._data.userInfo = data.userInfo
            this._data.hasToken = true
            this._data.currentScene = 'hall'
            cc.sys.localStorage.setItem('userToken', data.token)
        } catch ({message}) {
            this.showToast(message);
        }


        // await this.showToast('登录成功')
        // this.goToHall()
    }

}
